package com.BigDate.server.service.impl;

import com.BigDate.server.mapper.ForkProjectMapper;
import com.BigDate.server.mapper.ProjectMapper;
import com.BigDate.server.mapper.UserMapper;
import com.BigDate.server.pojo.ForkProject;
import com.BigDate.server.pojo.Project;
import com.BigDate.server.pojo.RespPageBean;
import com.BigDate.server.pojo.User;
import com.BigDate.server.service.IProjectService;
import com.BigDate.server.utils.MarkDownUtils;
import com.BigDate.server.utils.Md5Util;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stx
 * @since 2021-12-22
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private ForkProjectMapper forkProjectMapper;
    /**
     * 获取项目(分页)
     * @param currentPage
     * @param size
     * @param project
     * @return
     */
    @Override
    public RespPageBean getProjectByPage(Integer currentPage, Integer size, Project project) {
        Page<Project> page = new Page<>(currentPage,size);
        IPage<Project> projectIPage = projectMapper.getProjectByPage(page,project);
        RespPageBean respPageBean = new RespPageBean(projectIPage.getTotal(),projectIPage.getRecords());
        return respPageBean;
    }

    /**
     * 获取内容
     * @param id
     * @return
     */
    @Override
    public String getContent(Integer id) {
        Project project = projectMapper.selectById(id);
        String s = MarkDownUtils.openFile(project.getContent());
        return s;
    }

    @Override
    public List<Project> findLimitPr() {
        return projectMapper.findLimitPr();
    }

    @Override
    public List<Project> findLimitIt() {
        return projectMapper.findLimitIt();
    }

    @Override
    public List<Project> findLimitHi() {
        return projectMapper.findLimitHi();
    }

    @Override
    public Project findById(Integer id) {
        return projectMapper.findById(id);
    }


    @Override
    public void runPython(Integer projectId, Integer userId) {
        Project project = projectMapper.selectById(projectId);
        User user = userMapper.selectById(userId);
        user.setPassword(null);

        long time = System.currentTimeMillis();
        String defPath = "C:\\Users\\11920\\.jupyter\\";
        String userDir = projectId+user.getUsername()+time;
        File userWork = new File(defPath+userDir);
        userWork.mkdir();
        try {
            MarkDownUtils.downIpynb(project.getPynbPath(),project.getContent(),defPath+userDir+"\\");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String cmd = "jupyter lab --notebook-dir "+defPath+userDir;
        try {
            Process p = Runtime.getRuntime().exec(cmd);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ForkProject forkProject = new ForkProject();
        forkProject.setProjectid(projectId);
        forkProject.setUserid(userId);
        forkProject.setForktime(new Date());
        forkProjectMapper.insert(forkProject);
    }
}
